var app=getApp()
const md5 = require('../../md5.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    password: "",
    login_msg: "",
    openid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    if(options.username != undefined){
      that.setData({
        username: options.username 
      });
    }
    //参数传递获取的openid
    that.setData({
      openid:options.openid
    });
  },

  /**
   * 登录
   */
  bindToLogin: function () {
    let that = this;
    let username = that.data.username;
    let password = that.data.password;
    if(username == "" || password == ""){
      wx.showModal({
        title: '登录警告',
        content: '用户名密码不能为空！',
      });
      return;
    }

    that.load("请稍等");
    var header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      // url: 'https://119.3.46.145/server/mlogin?name=' + that.data.username + '&pwd=' + md5.hex_md5(password) + '&appid=' + app.globalData.AppID + '&openid=' + app.globalData.openid,
      //url:'https://119.3.46.145/server/mWebLogin',
      //url:'https://119.3.130.227/server/mWebLogin',      
      url:app.globalData.url+'mWebLogin',
      method: 'POST',
      data:{
        name: that.data.username,
        pwd: md5.hex_md5(password) 
      },
      header: header,
      success: res => {
        that.disLoad();
        //console.log(res, "res")

        //返回数据{"nick":"test","name":"test@126.com","error":"0","userid":"922aeb81-29c8-41ba-9f82-36d9abb33265"}
       

        //app.globalData.login_name = username;

        if(res.data.error == "-1"){
          wx.showModal({
            title: '错误',
            content: '用户名或者密码错误!',
          });
          return;
        }

        app.globalData.is_login = true;
        app.globalData.userid = res.data.userid;
        app.globalData.login_nick = res.data.nick;
        app.globalData.login_name = res.data.name;
        app.globalData.login_error = res.data.error;

        wx.setStorageSync('username',  that.data.username);
        wx.setStorageSync('password',  md5.hex_md5(password));
        wx.setStorageSync('userid',  res.data.userid);

        //获取设备
        let header = {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': "openid=" + app.globalData.openid
        };

        //获取设备列表的协议 console.log(app.globalData.is_login, "app.globalData.is_login "); //获取分组
        // if (app.globalData.is_login === true) {
        //   wx.request({
        //     url: "https://119.3.46.145/server/FindDevice?userid=" + app.globalData.userid + "&appid=" + app.globalData.AppID + "&openid=" + app.globalData.openid,
        //     header: header,
        //     method: 'POST',
        //     success: res => {
        //       console.log(res, "res");
        //       app.globalData.dev_list = JSON.parse(res.data.devices);

        //       //把设备列表，组织称 markers列表 这一步markers内容比较少 [{devNo:'', description:''},{}]
        //       let local_markers = [];
             
        //       app.globalData.dev_list.forEach(function(item, index){
        //         let tmp_json = {};
        //         tmp_json.id = item.devNo;
        //         tmp_json.name = item.description; 
        //         tmp_json.groupId = item.groupId;
        //         local_markers.push(tmp_json);                 
        //       });
        //       app.globalData.markers = local_markers;            
              
        //     },
        //     fail: function (error) {
        //       console.log(error, "error");
        //     },
        //     complete: function (complete) {

        //     }
        //   });


        //   wx.request({
        //     url: "https://119.3.46.145/server/FindGroup?userid=" + app.globalData.userid,
        //     header: header,
        //     method: 'POST',
        //     success: res => {
        //       app.globalData.global_group = JSON.parse(res.data.data);             
        //     },
        //     fail: function (error) {
        //       console.log(error, "login FindGroup error");
        //     }
        //   })
        // }   
 


        // wx.navigateBack({
        //   delta: 1
        // });
        
        //wx.switchTab({
          //url: '../index/index',         
        //})

        wx.navigateTo({
          url:'../controlBooth/controlBooth'
        })

      
        // console.log(app.globalData, "app.globalData");


        //0表示注册成功,1表示注册的用户名已存在
        // if (res.data.error === "0") {
        //   wx.navigateTo({
        //     url: '../login/login',
        //   });
        // }
        // if (res.data.error === "1") {
        //   wx.showModal({
        //     title: '注册警告',
        //     content: '该用户名已注册，请找回或者重新注册新账号',
        //   })
        // }

      },
      fail: function (error) {
        //console.log(error, "login error");
        wx.showModal({
          title: '提示',
          content: JSON.stringify(error),
        })
        that.disLoad();
      },
      complete: function (complete) {

      }
    })
  },

//表单 登陆
  formSubmit:function(e){
    let that = this;
    that.setData({
      username: e.detail.value.input_username.trim(),
      password: e.detail.value.input_password.trim()
    });

    that.bindToLogin();
  },

  //用户名
  bindInputName: function (e) {
    let that = this;
   
    that.setData({
      username:e.detail.value.trim()
    });
  },

  bindInputPwd: function (e) {
    let that = this;
    that.setData({
      password:e.detail.value.trim()
    });
  },



  /**
   * 注册
   */
  bindToRegister: function (e) {
    wx.navigateTo({
      url: '../register/register',
    })

  },

  formReset:function(){
    let that = this;
    that.bindToRegister();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  load: function (s) {
    wx.showLoading({
      title: s,
    });
  },

  disLoad: function () {
    wx.hideLoading();
  }
})